// pages/enterprise/index/news/index.js
import {getEnterpriseResumeList, getEnterpriseOtherList, getTipsCount} from "../../../../service/Message";
import Config from "../../../../main.config";
import {getStorageSync} from "../../../../utils/Storage";
Component({

    /*
    * 默认接收全局样式
    * */
    options: {
        addGlobalClass: true,
        styleIsolation: "shared",
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        _tabs: {
            active: 0,
            list: [{
                title: "简历消息",
                info: 0
            }, {
                title: "员工消息",
                info: 0
            }]
        },
        _resume: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
        _personnel: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        }
    },

    lifetimes: {
        attached: async function () {
            let active = this.data._tabs.active;
            this.data._registerId = getStorageSync(Config.storage.registerInfo + ".id");
            await this.getNewMessagesCount();
            if (active === 0) {
                this.setData({"_resume.loading": true})
            } else {
                this.setData({"_personnel.loading": true})
            }
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*
       * 获取消息数量
       * */
        getNewMessagesCount: async function () {
            try {
                let registerId = this.data._registerId;
                let deliveryNum = await getTipsCount({
                    messageUserid: registerId,
                    types: "0",
                    status: 0
                })
                let otherNum = await getTipsCount({
                    messageUserid: registerId,
                    types: "4,5",
                    status: 0
                })
                this.setData({
                    "_tabs.list[0].info": deliveryNum,
                    "_tabs.list[1].info": otherNum
                })
            } catch (e) {
                console.log(e)
            }
        },


        /*
          * tabs切换
          * */
        onTabsChange: function (e) {
            let active = this.data._tabs.active;
            let value = e.detail.index;
            if (active === value) return false;
            if (value === 0) {
                this.setData({
                    "_resume.reset": true,
                    "_tabs.active": value
                })
            } else {
                this.setData({
                    "_personnel.reset": true,
                    "_tabs.active": value
                })
            }
            this.getNewMessagesCount()
        },

        /*
         * 列表加载
         * */
        onLoadList: async function (e) {
            let current = this.data._tabs.active;
            let registerId = this.data._registerId;
            let name = current === 0 ? "_resume" : "_personnel";
            let res = [], newList = [];
            try {
                let paging = e.detail.paging;
                let list = this.data[name].list;
                let isReset = this.data[name].reset;
                if (current === 0) {
                    res = await getEnterpriseResumeList({
                        page: paging.page,
                        limit: paging.pageSize,
                        messageUserid: registerId
                    });
                    newList = res.data.map(function (item) {
                        let logoUrl = Config.api.downLoad + "?fileId=" + item.logo;
                        return {
                            id: item.id,
                            positionName: item.positionName,
                            nature: item.nature,
                            schoolName: item.schoolName,
                            salary: item.salary,
                            professionalName: item.professionalName,
                            studentLogo: logoUrl,
                            studentName: item.studentName,
                            staffName: item.staffName,
                            name: item.name,
                            resume: item.resume,
                            createTime: item.createTime,
                            status: item.status
                        }
                    });
                } else {
                    res = await getEnterpriseOtherList({
                        page: paging.page,
                        limit: paging.pageSize,
                        messageUserid: registerId
                    })
                    newList = res.data.map(function (item) {
                        let logoUrl = Config.api.downLoad + "?fileId=" + item.staffLogo;
                        return {
                            id: item.id,
                            createTime: item.createTime,
                            staffLogo: logoUrl,
                            staffName: item.staffName,
                            staffId: item.staffId,
                            applyStatus: parseInt(item.applyStatus),
                            phone: item.phone,
                            type: item.type,
                            typeName: item.type === 4 ? "申请加入公司" : "退出公司",
                            status: item.status
                        }
                    });
                }
                list = isReset ? newList : list.concat(newList);
                this.setData({
                    [name + ".list"]: list,
                    [name + ".total"]: res.count,
                    // ["_tabs.list[" + current + "].info"]: res.count || 0
                });
            } catch (e) {
                this.setData({
                    [name + ".error"]: true
                })
            } finally {
                this.setData({
                    [name + ".reset"]: false,
                    [name + ".loading"]: false
                })
            }
        },

        //上拉加载更多
        onScrollToLower: function () {
            let active = this.data._tabs.active;
            if (active === 0) {
                this.setData({"_resume.loading": true})
            } else {
                this.setData({"_personnel.loading": true})
            }
        },

    }
})
